package com.team.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.dao.DeptMapper;
import com.team.entity.Dept;
import com.team.entity.Section;
import com.team.service.DeptService;
import com.team.vo.Cascader;
import com.team.vo.DeptVo;
import com.team.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : 陈灰灰
 * @Date: 2022/10/13/19:20
 * @Description:
 */
@Service
public class DeptServiceImpl implements DeptService {
    private int status;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Result selectAllDepts() {
        List<Dept> deptsList = deptMapper.queryAllDepts();
        return new Result(200,"查询成功",deptsList);

    }

    @Override
    public Result selectAllFormatting() {
        List<Dept> deptsList = deptMapper.queryAllDepts();
        List<Cascader> first = new ArrayList<>();
        for (Dept dept : deptsList) {
            Cascader cascader = new Cascader();
            cascader.setValue(String.valueOf(dept.getId()));
            cascader.setLabel(dept.getName());
            List<Cascader> two = new ArrayList<>();
            for (Section section : dept.getSections()) {
                Cascader children = new Cascader();
                children.setValue(String.valueOf(section.getId()));
                children.setLabel(section.getName());
                two.add(children);
            }
            cascader.setChildren(two);
            first.add(cascader);
        }
        return new Result(200,"查询成功",first);
    }

    /**
     * 根据科室id查询部门
     * @param deptId
     * @return
     */
    @Override
    public Result selectBydeptId(int deptId) {
        List<Dept> depts = deptMapper.selectDept(deptId);
        return new Result(200,"查询成功",depts);
    }

    /**
     * 修改部门状态
     * @param deptId
     * @return
     */
    @Override
    public Result updatestatus(Dept deptId) {
        if (deptId.getStatus()==1){
            deptId.setStatus(0);
        }else {
            deptId.setStatus(1);
        }
        int i = deptMapper.updateStatus(deptId);
        return new Result(200,"修改成功",i);

    }

    /**
     * 添加部门
     * @param dept
     * @return
     */
    @Override
    public Result insertAll(Dept dept) {
        deptMapper.insertAll(dept);
        return new Result(200,"添加成功");
    }

    @Override
    public Result selectAllDept(int currentPage, int pageSize, DeptVo deptVo) {
        if (deptVo.getCreateTime()!=null){
            deptVo.setOpentime(deptVo.getCreateTime()[0]);
            deptVo.setEndtime(deptVo.getCreateTime()[1]);
        }
        PageHelper.startPage(currentPage,pageSize);
        List<Dept> depts = deptMapper.selectAllDept(deptVo);
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result deleteByPrimaryKey(Integer id) {
        int i = deptMapper.deleteByPrimaryKey(id);
        return new Result(200,"删除成功",i);
    }

}
